package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Integral;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.IntegralService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Integral)积分表控制层
 *
 * @author zjx
 * @since 2021-05-14 19:42:02
 */
@Api(value = "积分表接口",description = "用于对积分表的CRUD")
@RestController
@RequestMapping("integral")
public class IntegralController {
    /**
     * 服务对象
     */
    @Resource
    private IntegralService integralService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条积分数据")
    @GetMapping("selectOne")
    public ResponseEntity selectOne(@ApiParam("主键")Integer id) {
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(200);
        responseEntity.setMessage("查询单条积分数据成功");
        responseEntity.setData(this.integralService.queryById(id));
        System.out.println(this.integralService.queryById(id).getCreateTime());
        return responseEntity;
    }

    /**
     * 分页查询
     *
     * @param startpage 查询起始位置
     * @param pagesize 查询条数
     * @return 分页信息
     */
    @ApiOperation("查询积分分页信息")
    @GetMapping("selectAll")
    public ResponseEntity selectAll(@ApiParam("起始页") int startpage, @ApiParam("每条总页数") int pagesize){
        PageHelper.startPage(startpage,pagesize);
        PageInfo<Integral> pageInfo = new PageInfo<>(this.integralService.queryAllByLimit(startpage, pagesize));
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(200);
        responseEntity.setMessage("查询分页信息成功");
        responseEntity.setData(pageInfo);
        return responseEntity;
    }
    /**
     * 新增一条积分信息
     *
     * @param integral 积分信息
     * @return 积分信息
     */
    @ApiOperation("新增一条积分数据")
    @RequestMapping("addIntegral")
    public ResponseEntity addIntegral(@ApiParam("积分信息实体") @RequestBody Integral integral){
        Integral insert = this.integralService.insert(integral);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(200);
        responseEntity.setMessage("新增积分数据成功");
        responseEntity.setData(insert);
        return responseEntity;
    }

    /**
     * 通过id删除积分信息
     *
     * @param id 积分id
     * @return  布尔值,是否成功
     */
    @ApiOperation("通过主键删除积信息")
    @RequestMapping("deleteIntegral")
    public ResponseEntity deleteIntegral(@ApiParam("主键") int id){
        boolean result = this.integralService.deleteById(id);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(200);
        responseEntity.setMessage("删除积分信息成功");
        responseEntity.setData(result);
        return responseEntity;
    }

    /**
     * 通过传入的积分信息实体类修改信息,并返回修改后的积分信息实体
     *
     * @param integral 积分信息实体类
     * @return 积分信息实体类
     */
    @ApiOperation("修改积分信息")
    @RequestMapping("updateIntegral")
    public ResponseEntity updateIntegral(@ApiParam("积分信息实体")@RequestBody Integral integral){
        Integral update = this.integralService.update(integral);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(200);
        responseEntity.setMessage("修改积分信息成功");
        responseEntity.setData(update);
        return responseEntity;
    }

}
